package com.dongqi.myblog.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongqi.myblog.po.TBlog;
import com.dongqi.myblog.po.TTag;
import com.dongqi.myblog.po.vo.TagQuery;
import com.dongqi.myblog.service.TBlogService;
import com.dongqi.myblog.service.TTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dongqi
 * @since 2020-08-03
 */
@Controller
public class TTagController {

    @Autowired
    private TTagService tagService;
    @Autowired
    private TBlogService blogService;

    /**
     * 前台跳转到标签页的接口
     */
    @RequestMapping("/tag/{id}")
    public String tags(@RequestParam(defaultValue = "1")Long pageNo,
                       @RequestParam(defaultValue = "10")Long pageSize,
                       @PathVariable Long id,
                       Model model){

        List<TagQuery> tags = tagService.getTagIndex(10000);
        if (id==-1){
            id = tags.get(0).getId();
        }
        Page<Object> objectPage = new Page<>(pageNo, pageSize);
        System.out.println(objectPage.getTotal());
        Page<TBlog> page = blogService.blogsByTag(new Page<>(pageNo, pageSize),id);
        System.out.println(page.getTotal());
        model.addAttribute("tags",tags);
        model.addAttribute("page",page);
        model.addAttribute("activeTagId",id);
        model.addAttribute("hasPrevious",page.hasPrevious());
        model.addAttribute("hasNext",page.hasNext());
        return "tags";
    }

}

